<?php

class PublicAction extends BaseAction {

    public function verify() {
        import("ORG.Util.Image");
        Image::buildImageVerify(4, 1, "png", "60", "26");
    }

    public function login() {
        $module = $this->_param("module");
        $action = $this->_param("action");
        $this->assign("module", $module);
        $this->assign("action", $action);
        $this->display();
    }

    public function checklogin() {
        $url = $_POST['url']; //获取上一页面链接
        if (!$url) {
            $url = '/';
        }
        $url = str_replace("index.php/","",$url); 
        
        $param_name = $this->_param('name');
        $param_password = $this->_param('pwd');
        $param_verify = $this->_param('verify');
        $module = $this->_param("module");
        $module = !empty($module) ? $module : "Index";
        $action = $this->_param("action");
        $action = !empty($action) ? $action : "index";
        if (md5($param_verify) != $_SESSION['verify']) {
            $this->error("验证码错误");
        }
        $user = M("Users");
        $result = $user->where("(name='{$param_name}' or phone='{$param_name}') and pwd='" . md5($param_password) . "'")->select();
        if ($result) {
            $_SESSION['userid'] = $result[0]['id'];
            $_SESSION['name'] = $result[0]['name'];
            $_SESSION['phone'] = $result[0]['phone'];
            $_SESSION['qq'] = $result[0]['qq'];
            $_SESSION['type'] = $result[0]['type'];
            $_SESSION['money'] = $result[0]['money'];
//            $this->redirect("/{$module}/{$action}");
            $this->redirect($url); //登录成功后跳转到上一页面
            
//            $this->success("dsfa",$url);
        } else {
            $this->error("账号或密码输入有误");
        }
    }

    public function register() {
        $errno = $this->_param("errno");
        if (!empty($errno))
            $this->assign("errno", $errno);
        $this->display();
    }

    public function register_do() {
        $user = M('Users');
        $name = $this->_param('name');
        $password = $this->_param('password');
        $rpassword = $this->_param('rpassword');
        $phone = $this->_param("phone");
        $type = $this->_param("type");
        $code = $this->_param("code");
		$qq = $this->_param("qq");
        if (!empty($name)) {
            $data['name'] = $name;
        } else {
            $this->error("用户名不得为空");
        }
        if (!empty($password) && $password == $rpassword) {
            $data['pwd'] = md5($password);
        } else {
            $this->error("密码输入有误");
        }
        if (!empty($phone)) {
            $result = $user->getByPhone($phone);
            if ($result) {
                $this->redirect("/Public/register", array('errno' => 1), 0);
            }
            $data['phone'] = $phone;
        } else {
            $this->error("手机号码必须输入");
        }
        if ($code != $_SESSION['sms_verify']) {
            $this->error("验证码输入错误");
        }
        $data['type'] = $type;
        $list = $user->add($data);
        if ($list) {
		    $_SESSION['userid'] = $list;
            $_SESSION['name'] = $name;
            $_SESSION['phone'] = $phone;
            $_SESSION['qq'] = $qq;
            $_SESSION['type'] = $type;
            $_SESSION['money'] = 0;
            $this->redirect("/Public/regok");
        } else {
            $this->error("注册失败");
        }
    }

    public function checkname($name = '') {
        if (empty($name)) {
            $this->error("用户名不得为空");
        }
        $user = M("Users");
        $result = $user->getByName($name);
        if (!$result) {
            $this->error("用户名不存在");
        } else {
            $this->success("用户名存在");
        }
    }

    public function checkphone($phone = '') {
        if (empty($phone)) {
            $this->error("手机号码不得为空");
        }
        $user = M("Users");
        $result = $user->getByName($name);
        if ($result) {
            $this->error("该手机号码已经被注册");
        }
    }

    //发送手机验证码	
    public function sendsms() {
        $phone = $this->_param("phone");
        $verify = rand(0, 9) . rand(0, 9) . rand(0, 9) . rand(0, 9) . rand(0, 9) . rand(0, 9);
        $_SESSION['sms_verify'] = $verify;
        $name = "wxr";
        $pwd = "wxr19890203";
        $msg = rawurlencode("欢迎您加入模板返利网，您的注册验证码是【{$verify}】。");
        $res = file_get_contents("http://www.gysoft.cn/smspost_utf8/send.aspx?username={$name}&password={$pwd}&mobile={$phone}&content={$msg}");
        $this->success($res);
    }
	
	public function changephonesms(){
		$phone = $this->_param("phone");
        $verify = rand(0, 9) . rand(0, 9) . rand(0, 9) . rand(0, 9) . rand(0, 9) . rand(0, 9);
        $_SESSION['sms_verify'] = $verify;
        $name = "wxr";
        $pwd = "wxr19890203";
        $msg = rawurlencode("来自模板返利网的消息：您正在修改绑定手机号码，验证码：【{$verify}】。");
        $res = file_get_contents("http://www.gysoft.cn/smspost_utf8/send.aspx?username={$name}&password={$pwd}&mobile={$phone}&content={$msg}");
        $this->success($res);
		}

    public function checkverify() {
        $verify = $this->_param("verify");
        if (md5($verify) == $_SESSION['verify']) {
            $this->success("ok");
        }
    }

    public function logout() {
        unset($_SESSION['userid']);
        unset($_SESSION['name']);
        unset($_SESSION['qq']);
        unset($_SESSION['phone']);
        unset($_SESSION['type']);
        unset($_SESSION['money']);
        $this->redirect("/Index/index");
    }

}

?>